package com.fly.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class StreamDemo
{
    private static List<String> list = new ArrayList<String>();
    
    @BeforeAll
    public static void init()
    {
        for (int i = 0; i < 20; i++)
        {
            list.add(RandomStringUtils.randomAlphabetic(3, 8));
        }
        list.forEach(System.out::println);
        System.out.println();
        
        list.forEach(n -> System.out.println(n));
        log.info("@BeforeAll end");
    }
    
    @Test
    public void test1()
    {
        list.forEach(System.out::println);
    }
    
    @Test
    public void test()
    {
        log.info("test");
        List<String> list2 = list.stream().filter(s -> s.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList());
        list2.stream().forEach(System.out::println);
        
        log.info("step2");
        System.out.println(ToStringBuilder.reflectionToString(list, ToStringStyle.MULTI_LINE_STYLE));
        System.out.println(list);
        log.info("test end");
    }
    
    @Test
    public void test2()
    {
        Predicate<String> startsWithJ = (n) -> n.startsWith("A");
        Predicate<String> fourLetterLong = (n) -> n.length() >= 4;
        list.stream().filter(startsWithJ.and(fourLetterLong)).forEach((n) -> System.out.print("\nName, which starts with 'A' and four letter long is : " + n));
    }
}
